//
//  ActivityViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/8.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "ActivityViewController.h"
#import "ActivityTableViewCell.h"
#import "ActivityDetailViewController.h"

@interface ActivityViewController ()

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,copy) NSString *activityType;//1代表户外 2代表室内
@property (nonatomic,copy) NSString *time;//时间 ,正序 传 ASC , 倒序DESE
@property (nonatomic,copy) NSString *fee;//正序 传 ASC , 倒序 传 DESC
@end

@implementation ActivityViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    //    [Global setStatusBarBackgroundColor:[UIColor clearColor]];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"活动招募";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = KBACKGROUNDCOLOR;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ActivityTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"ActivityTableViewCell"];
    [self creatDataList];
    [self creatHeadView];
}
- (void)creatHeadView{
    UIView *headView = [[UIView alloc]init];
    headView.backgroundColor = [UIColor whiteColor];
    headView.frame = CGRectMake(0, 0, kScreenW, 60);
    self.tableView.tableHeaderView = headView;
    
    NSArray *textArray = @[@"户外",@"时间",@"费用"];
    for (int i = 0; i < 3; i ++) {
        CustomeButton *customeButton = [[CustomeButton alloc]init];
        customeButton.frame = CGRectMake( kScreenW / 3 * i, 20, kScreenW / 3, 20);
        customeButton.relative = KRelativeAlignmentLeft;
        customeButton.titleRect = CGRectMake((kScreenW/3 - 70)/2, 0, 40, 20);
        customeButton.imageRect = CGRectMake(CGRectGetMaxX(customeButton.titleRect) + 5, 5, 14, 9);
        [customeButton setTitle:textArray[i] forState:UIControlStateNormal];
        [customeButton setTitleColor:KTEXTMAINCOLOR forState:UIControlStateNormal];
        [customeButton setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
        customeButton.tag = 1000 + i;
        [customeButton addTarget:self action:@selector(selectList:) forControlEvents:UIControlEventTouchUpInside];
        [headView addSubview:customeButton];
    }
}
- (void)selectList:(UIButton *)sender{
    sender.selected = !sender.selected;
    [sender setImage:sender.selected?[UIImage imageNamed:@"arrow_up"]:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
    switch (sender.tag) {
        case 1000:{
            [sender setTitle:sender.selected?@"户内":@"户外" forState:UIControlStateNormal];
            self.activityType = sender.selected?@"2":@"1";
        }
            break;
        case 1001:
            self.time = sender.selected?@"DESC":@"ASC";
            break;
        case 1002:
            self.fee = sender.selected?@"DESC":@"ASC";
            break;
        default:
            break;
    }
    [self setupRefresh];
}
#pragma mark - 获取数据
- (void)creatDataList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:@"1" forKey:@"page"];   //页数 默认1
    [dic setValue:KREFRESHROWS forKey:@"rows"];   //每页数量
    [dic setValue:@"5" forKey:@"businessType"]; //5 活动招募
    [dic setValue:strIsEmpty(self.activityType)?@"1":self.activityType forKey:@"type"]; //5 活动招募
    [dic setValue:strIsEmpty(self.time)?@"ASC":self.time forKey:@"time"]; //5 活动招募
    [dic setValue:strIsEmpty(self.fee)?@"ASC":self.fee forKey:@"fee"]; //5 活动招募
    [yjNetWorkManger requestType:GET andURL:[JSPathUtil urlAppHomePageGetActivityPage] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            NSArray *array = [NSArray arrayWithArray:response];
            if (self.pageNo == 1) {
                [self.dataArray removeAllObjects];
            }
            for (int i = 0; i < [response count]; i++) {
                [self.dataArray addObject:[ActivityInfo mj_objectWithKeyValues:array[i]]];
            }
//            [self refreshCount:self.dataArray.count noDataText:@"还没有活动招募" totalRow:response[@"totalRow"]];
            [self.tableView reloadData];
        }
    }];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ActivityTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ActivityTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.activityInfo = self.dataArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ActivityInfo *activityInfo = self.dataArray[indexPath.row];
    ActivityDetailViewController *activityDetailVC = [[ActivityDetailViewController alloc]init];
    activityDetailVC.activityId = activityInfo.fileEntryId;
    [self.navigationController pushViewController:activityDetailVC animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 10);
    //    view.backgroundColor = KBACKGROUNDCOLOR;
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectZero;
    //    view.backgroundColor = KBACKGROUNDCOLOR;
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
